#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>

class Inet_Addr
{
public:
    Inet_Addr(struct sockaddr_in &addr)
    {
        // ip = inet_ntoa(addr.sin_addr); // char *inet_ntoa(struct in_addr in);
        // inet_ntoa这个函数的返回值是存放到函数内部的静态区的，如果大量的多线程来访问，会导致该区域被覆盖，导致线程不安全
        char inbuffer[1024];
        inet_ntop(AF_INET, ip.c_str(), inbuffer, sizeof(inbuffer));
        ip = inbuffer;
        port = ntohs(addr.sin_port); // 网络字节序转主机字节序
    }

    std::string Getip()
    {
        return ip;
    }

    uint16_t Getport()
    {
        return port;
    }

    std::string PrintDebug()
    {
        std::string info;
        info += ip;
        info += ":";
        info += std::to_string(port);
        return info;
    }

private:
    std::string ip;
    u_int16_t port;
};
